// Registry.cpp
// (c) 2008, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "Registry.h"

#include <string.h>
#include <shlwapi.h>


namespace RRO
{
// -- RegistryException class --

    //
    // Constructor with a registry call error code.
    //
    RegistryException::RegistryException(LONG p_ErrorCode)
        : Win32Exception(p_ErrorCode)
    {
    }

    //
    // Copy constructor.
    //
    RegistryException::RegistryException(const RegistryException& p_Exception)
        : Win32Exception(p_Exception)
    {
    }

    //
    // Assignment operator.
    //
    RegistryException& RegistryException::operator=(const RegistryException& p_Exception)
    {
        if (this != &p_Exception) {
            Win32Exception::operator=(p_Exception);
        }
        return *this;
    }

    //
    // Destructor.
    //
    RegistryException::~RegistryException(void)
    {
    }

    //
    // Returns exception's description.
    //
    const char* RegistryException::what(void) const
    {
        return "RRO::Registry::RegistryException";
    };

// -- RegistryKey class --

    //
    // Constructor with null key.
    //
    RegistryKey::RegistryKey(void)
        : m_hKey(0)
    {
    }

    //
    // Constructor with specific key. The key will be closed by the destructor.
    //
    RegistryKey::RegistryKey(HKEY p_hKey)
        : m_hKey(p_hKey)
    {
    }

    //
    // Destructor.
    //
    RegistryKey::~RegistryKey(void)
    {
        // Close key if we have one.
        if (m_hKey != 0) {
            ::RegCloseKey(m_hKey);
        }
    }

    //
    // @return Handle to stored key. Can be null.
    //
    HKEY RegistryKey::Key(void) const
    {
        return m_hKey;
    }

    //
    // @return Handle to stored key. Can be null.
    //
    RegistryKey::operator HKEY(void) const
    {
        return m_hKey;
    }

    //
    // Returns the address where we store the handle of the key we keep.
    // NOTE: using this to set the key can be dangerous if a key is already stored.
    // Use to pass this object to a Win32 registry call only.
    //
    HKEY* RegistryKey::operator&(void)
    {
        return &m_hKey;
    }

// -- Registry class --

    //
    // Sets a string value in the given registry key. If the key doesn't exist, it is created.
    //
    // @param p_hBaseKey Handle of base key. Usually a predefined key like HKEY_CURRENT_USER.
    // @param p_Path Path of the key to create in the registry, relatively to the base key.
    //               Can be null to set a value in the base key itself (if allowed).
    // @param p_ValueName Name of the value to set. Can be null to set the default value.
    // @param p_Value Value to set.
    //
    void Registry::SetStringValue(HKEY p_hBaseKey, const char* p_Path,
                                  const char* p_ValueName, const char* p_Value)
    {
        // Create the key where to store the value. This will create all levels if required, and
        // will simply open the key if it already exists.
        RegistryKey key;
        LONG res = ::RegCreateKey(p_hBaseKey, p_Path, &key);
        if (res != ERROR_SUCCESS) {
            throw RegistryException(res);
        }

        // Now set the value.
        res = ::RegSetValueEx(key, p_ValueName, 0, REG_SZ,
            reinterpret_cast<const BYTE*>(p_Value),
            ::strlen(p_Value) + sizeof(char));
        if (res != ERROR_SUCCESS) {
            throw RegistryException(res);
        }

        // Key is closed automatically by the stack-based class.
    }

    //
    // Deletes the given registry key.
    //
    // @param p_hBaseKey Handle of base key. Usually a predefined key like HKEY_CURRENT_USER.
    // @param p_Path Path of the key to delete in the registry, relatively to the base key.
    //
    void Registry::DeleteKey(HKEY p_hBaseKey, const char* p_Path)
    {
        // SHDeleteKey can delete keys recursively, saving us the trouble.
        LONG res = ::SHDeleteKey(p_hBaseKey, p_Path);
        if (res != ERROR_SUCCESS) {
            throw RegistryException(res);
        }
    }

}; // namespace RRO
